package www.golf.web.gonwubu;

import java.sql.SQLException;
import java.util.List;

import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import www.golf.ibatis.bean.Subcontractor;
import www.golf.ibatis.bean.Supervision;
import www.golf.web.session.WebSession;
import www.golf.web.util.UtilListbox;
import www.golf.web.util.UtilZKObject;

public class SupervisionControl extends WebSession{
	UtilZKObject ZKObj =new UtilZKObject(this);
	UtilListbox lb= new UtilListbox();
	public void initialize() {
		List list=null;
		try {
			list = (List)dbSession().queryForList("Supervision.selectSupervision");
			dbSessionClose();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		lb.setComponentData(this, "grid", list,dataRender());
	}
	public ListitemRenderer dataRender(){
		 return new ListitemRenderer(){
				public void render(Listitem item, Object data) throws Exception {
					Supervision form = (Supervision) data;
					Listcell name = new Listcell();
					name.setLabel(form.getName());
					name.setParent(item);		
					
					Listcell con_person = new Listcell();
					con_person.setLabel(form.getConnect_person());
					con_person.setParent(item);		
					
					Listcell address = new Listcell();
					address.setLabel(form.getAddress());
					address.setParent(item);	
					
					Listcell phone = new Listcell();
					phone.setLabel(form.getPhone());
					phone.setParent(item);	
					
					Listcell pid = new Listcell();
					pid.setLabel(String.valueOf(form.getPid()));
					pid.setParent(item);
					pid.setVisible(false);
				}		
			};
	}
	
	public void onEdit(){
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null){
			try {
				final int key =Integer.parseInt(value);
				final Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
				final UtilZKObject zobj= new UtilZKObject(win);
				Events.postEvent("onModal", win, null);
				setEditdata(zobj);
				win.getFellow("ed_submit").addEventListener("onClick", new EventListener(){
					public void onEvent(Event event) throws Exception {
						Supervision sb=setBean(zobj);
						sb.setPid(key);
						dbSession().update("Supervision.updateSupervision", sb);
						Messagebox.show("修改完毕");
						win.detach();
						initialize();
					}
				});
				win.getFellow("ed_cancel").addEventListener("onClick",new EventListener(){
					public void onEvent(Event event) throws Exception {
						win.detach();
					}
				});
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
	}
	public void onDelete(){
		String value=lb.getPKListcellValue(this, "grid");
		if(value!=null){
			int key =Integer.parseInt(value);
			try {
				if(Messagebox.show("删除可能造成系统混乱", "注意", Messagebox.YES | Messagebox.NO, Messagebox.QUESTION )==Messagebox.YES){
					dbSession().delete("Supervision.deleteSupervision",key);
					initialize();
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	public void onAdd() {
		final Window win=(Window)Executions.createComponents(getPagefileSession()+"edit.zul", null, null);
		final UtilZKObject zobj= new UtilZKObject(win);
		win.setTitle("新增資料");
		Events.postEvent("onModal", win, null);
		win.getFellow("ed_submit").addEventListener("onClick", new EventListener(){
			public void onEvent(Event event) throws Exception {
				
				dbSession().update("Supervision.insertSupervision", setBean(zobj));
				Messagebox.show("新增完畢");
				win.detach();
				initialize();
			}
		});
		win.getFellow("ed_cancel").addEventListener("onClick",new EventListener(){
			public void onEvent(Event event) throws Exception {
				win.detach();
			}
		});
	}
	public void setEditdata(UtilZKObject zobj){
		zobj.setObjectValue("ed_name", lb.getListcellValue(this, "grid", 0));
		zobj.setObjectValue("ed_con_person", lb.getListcellValue(this, "grid", 1));
		zobj.setObjectValue("ed_address", lb.getListcellValue(this, "grid", 2));
		zobj.setObjectValue("ed_phone", lb.getListcellValue(this, "grid", 3));
	}
	public Supervision setBean(UtilZKObject zobj){
		Supervision sb= new Supervision();
		sb.setName(zobj.getValue("ed_name"));
		sb.setConnect_person(zobj.getValue("ed_con_person"));
		sb.setAddress(zobj.getValue("ed_address"));
		sb.setPhone(zobj.getValue("ed_phone"));
		return sb;
	}
}